package com.ebupt.migu.music.global.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ebupt.migu.globalpopup.entity.ActivityApprove;
import com.ebupt.migu.globalpopup.entity.ActivityInfo;
import com.ebupt.migu.music.common.entity.Page;
import com.ebupt.migu.music.common.entity.ResultObject;
import com.ebupt.migu.music.common.exception.StatusEnum;
import com.ebupt.migu.music.global.mapper.GlobalApproveMapper;
import com.ebupt.migu.music.global.service.GlobalApproveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author liuyangyang@ebupt.com
 * @Description
 * @Date 2021/11/23 10:07
 */
@Slf4j
@Service
public class GlobalApproveServiceImpl implements GlobalApproveService {

    @Autowired
    private GlobalApproveMapper globalApproveMapper;

    @Override
    public ResultObject qryChecker() {
        ResultObject res = new ResultObject();
        List<Map<String, Object>> list = globalApproveMapper.qryChecker();
        res.setMsg("查询审批人成功");
        res.setData(list);
        return res;
    }

    @Override
    public ResultObject qryApproveList(Map<String, Object> map) {
        if ("1".equals(map.get("isAdmin")) || "1".equals(map.get("isBSLAuditor"))) {
            // 超级管理员有权查询所有审批列表
            map.remove("creatorId");
            map.remove("creatorDepartment");
        } else {
            map.remove("creatorId");
        }
        //活动状态：1-待审核；2-已驳回；3-未开始；4-进行中；5-已终止；6-已结束；7-已删除
        List<Byte> activityStatus = new ArrayList<>();
        // 获取指定的审批状态
        // 审批状态：1-待审批；2-已通过；3-已驳回；
        String approveStatus = String.valueOf(map.get("approveStatus"));
        switch (approveStatus) {
            case "1":
                activityStatus.add(Byte.valueOf("1"));
                break;
            case "3":
                activityStatus.add(Byte.valueOf("2"));
                break;
            case "2":
                activityStatus.add(Byte.valueOf("3"));
                activityStatus.add(Byte.valueOf("4"));
                activityStatus.add(Byte.valueOf("5"));
                activityStatus.add(Byte.valueOf("6"));
                break;
        }
        map.put("activityStatus", activityStatus);
        List<ActivityInfo> data = globalApproveMapper.qryApproveList(map);
        int total = globalApproveMapper.qryApproveListCount(map);
        // 分页
        Page<ActivityInfo> page = new Page<>();
        page.setTotal(total);
        page.setData(data);
        ResultObject res = new ResultObject();
        res.setMsg("查询审批列表成功");
        res.setData(page);
        return res;
    }

    @Override
    public ResultObject saveApprove(Map<String, Object> map) {
        ResultObject res = new ResultObject();
        // 验证是否是超管或者记录指定审批人
        //这里用了反选，将所有不可能的情况进行并集，那结果都是可能的
        if (!("1").equals(map.get("isBSLAuditor")) && !("1").equals(map.get("isAdmin")) &&
                !map.get("approveId").equals(map.get("creatorId"))) {
            res.setCode(StatusEnum.OPERATE_LIMITED.getCode());
            res.setMsg("当前用户无审批权");
            return res;
        }
        // 审批状态：1-待审批；2-已通过；3-已驳回；
        String approveStatus = String.valueOf(map.get("approveStatus"));
        String activityId = String.valueOf(map.get("activityId"));
        // 获取活动详情
        Map<String, Object> detail = globalApproveMapper.qryActivityInfo(activityId);
        ActivityApprove approve = new ActivityApprove();
        approve.setActivityId(activityId);
        approve.setActivityName(detail.get("activityName").toString());
        approve.setCreatorId(detail.get("creatorId").toString());
        approve.setCreatorName(detail.get("creatorName").toString());
        approve.setCreatorDepartment(detail.get("creatorDepartment").toString());
        // 获取操作人的信息为审批人的信息
        approve.setApproveId(map.get("creatorId").toString());
        approve.setApproveName(map.get("creatorName").toString());
        approve.setApproveStatus(approveStatus);
        approve.setApproveDesc(map.get("approveDesc").toString());
        // 更新审批记录
        globalApproveMapper.insert(approve);
        // 修改策略状态
        Map<String, Object> param = new HashMap<>();
        param.put("activityId", activityId);
        if ("2".equals(approveStatus)){
            param.put("activityStatus", 3);
        }else {
            param.put("activityStatus", 2);
        }
        globalApproveMapper.updateActivity(param);
        res.setCode(StatusEnum.SUCCESS.getCode());
        res.setMsg(StatusEnum.SUCCESS.getMsg());
        return res;
    }

    @Override
    public ResultObject qryApproveHistory(Map<String, Object> map) {
        ResultObject res = new ResultObject();
        List<ActivityApprove> activityApproves = globalApproveMapper.selectList(new QueryWrapper<ActivityApprove>().lambda()
                .eq(ActivityApprove::getActivityId,map.get("activityId").toString())
                .ne(ActivityApprove::getApproveStatus, 1));
        res.setCode(StatusEnum.SUCCESS.getCode());
        res.setMsg(StatusEnum.SUCCESS.getMsg());
        res.setData(activityApproves);
        return res;
    }
}
